﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class MTSlider : Slider, MTObject
    {
        #region MTObject Members

        public bool CanBeScaled { get { return MTProperties.GetCanBeScaled(this); } set { MTProperties.SetCanBeScaled(this, value); } }
        public bool CanBeDragged { get { return MTProperties.GetCanBeDragged(this); } set { MTProperties.SetCanBeDragged(this, value); } }
        public bool CanBeClicked { get { return MTProperties.GetCanBeClicked(this); } set { MTProperties.SetCanBeClicked(this, value); } }
        public bool CanBeRotated { get { return MTProperties.GetCanBeRotated(this); } set { MTProperties.SetCanBeRotated(this, value); } }
        /// <summary>
        /// Gets or sets a bool that describes the value of a AdornersEnabledProperty for the element.
        /// </summary>
        public bool AdornersEnabled { get { return MTProperties.GetAdornersEnabled(this); } set { MTProperties.SetAdornersEnabled(this, value); } }
        #endregion

        private Thumb thumb = null;
        private int _timeout = 1000;
        public event nl.tno.ict.multitouch.wpfmultitouch.events.NotMovedEventArgs NotMoved;
        public static readonly RoutedEvent MouseDoubleClickEvent;
        private int _fingerCount;

        public MTSlider()
        {
            // Sets the TouchEnabledObjectProperty for the element always true.
            MTProperties.SetTouchEnabled(this, true);
            this.Loaded += new RoutedEventHandler(MTSlider_Loaded);
            if (!this.IsEnabled)
            {
                thumb = (this.Template.FindName("PART_Track", this) as Track).Thumb;
                thumb.IsEnabled = false;
            }
        }

        static MTSlider()
        {
           MTSlider.MouseDoubleClickEvent = EventManager.RegisterRoutedEvent("SliderMouseDoubleClickEvent",
           RoutingStrategy.Bubble, typeof(RoutedEvent), typeof(MTCanvas));
        }

        public event RoutedEventHandler MouseDoubleClick
        {
            add { AddHandler(MTButton.MouseDoubleClickEvent, value); }
            remove { RemoveHandler(MTButton.MouseDoubleClickEvent, value); }
        }

        void MTSlider_Loaded(object sender, RoutedEventArgs e)
        {
            // todo
            try
            {
                thumb = (this.Template.FindName("PART_Track", this) as Track).Thumb;
            }
            catch { }
        }        

        public bool IsDragging
        {
            get { return MTProperties.GetIsDragging(this); }
            set { MTProperties.SetIsDragging(this, value); }
        }

        public bool IsScaling
        {
            get { return MTProperties.GetIsScaling(this); }
            set { MTProperties.SetIsScaling(this, value); }
        }

        public bool IsRotating
        {
            get { return MTProperties.GetIsRotating(this); }
            set { MTProperties.SetIsRotating(this, value); }
        }

       
        #region MTObject Members        

        void MTObject.NotMoved()
        {
            if (NotMoved != null)
                NotMoved(this, new EventArgs());
        }

        /// <summary>
        /// Gets or sets the timeout, in ms, used to by the NotMoved event. Default: 1000 ms
        /// </summary>
        int MTObject.Timeout
        {
            get { return _timeout; }
            set { _timeout = value; }
        }

        public int FingerCount
        {
            get
            {
                return _fingerCount;
            }
            set
            {
                _fingerCount = value;
            }
        }

        /// <summary>
        /// Gets or sets the onTop dependency property. When true, when a finger touches the object the object
        /// gets on top of the screen (z-index changes).
        /// </summary>
        public bool OnTop
        {
            get { return MTProperties.GetOnTop(this); }
            set { MTProperties.SetOnTop(this, value); }
        }

        /// <summary>
        /// Set the object TouchEnabled. When an object isn't touchEnabled interaction with fingers is not
        /// possible (nothing will happen).
        /// </summary>
        public bool TouchEnabled
        {
            get { return MTProperties.GetTouchEnabled(this); }
            set { MTProperties.SetTouchEnabled(this, value); }
        }

        #endregion
    }
}
